$(function () {


    $("#weeklyStartDate").datepicker({
        dateFormat: "yy-mm-dd",
        // timeFormat: "HH:mm:ss"
    })

    $("#weeklyEndDate").datepicker({
        dateFormat: "yy-mm-dd",
        // timeFormat: "HH:mm:ss"
    })

    $("#jqGrid").jqGrid({
        url: baseURL + 'deptmanage/weeklyinfo/list',
        datatype: "json",
        colModel: [
            {label: 'weeklyNo', name: 'weeklyNo', index: 'weekly_no', width: 50, key: true},
            {label: '人员序号', name: 'userseq', index: 'userseq', width: 80},
            {label: '本周周报', name: 'nowWeekly', index: 'now_weekly', width: 80},
            {label: '下周周报', name: 'nextWeekly', index: 'next_weekly', width: 80},
            {label: '周报开始日期', name: 'weeklyStartDate', index: 'weekly_start_date', width: 80},
            {label: '周报结束日期', name: 'weeklyEndDate', index: 'weekly_end_date', width: 80},
            {label: '创建日期', name: 'createDate', index: 'create_date', width: 80},
            {label: '创建人', name: 'createUser', index: 'create_user', width: 80},
            {label: '修改日期', name: 'updateDate', index: 'update_date', width: 80},
            {label: '修改人', name: 'updateUser', index: 'update_user', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        weeklyInfo: {},
        q: {
            key: null
        },
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.weeklyInfo = {};
        },
        update: function (event) {
            var weeklyNo = getSelectedRow();
            if (weeklyNo == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(weeklyNo)
        },
        saveOrUpdate: function (event) {
            var url = vm.weeklyInfo.weeklyNo == null ? "deptmanage/weeklyinfo/save" : "deptmanage/weeklyinfo/update";
            vm.weeklyInfo.weeklyEndDate=$("#weeklyEndDate").val();
            vm.weeklyInfo.weeklyStartDate=$("#weeklyStartDate").val();
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.weeklyInfo),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var weeklyNos = getSelectedRows();
            if (weeklyNos == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "deptmanage/weeklyinfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(weeklyNos),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (weeklyNo) {
            $.get(baseURL + "deptmanage/weeklyinfo/info/" + weeklyNo, function (r) {
                vm.weeklyInfo = r.weeklyInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'key': vm.q.key
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});